(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.ppfTradepartner.createVerifyController', createVerifyController);
	createVerifyController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'ppfTradepartnerService', '$filter' ];
	function createVerifyController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, ppfTradepartnerService, $filter) {
		var ctrl = this, ppfTradepartnerInfo = $scope.$stateParams.params, errorMessage = {};
		//ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.ppfTradepartner = {};
		
		ctrl.init = function() {
			var params = {
					taskId : ppfTradepartnerInfo.taskId
			};
			// init acct
			ppfTradepartnerService.initProfAcct(layoutConfig, $scope, ctrl);
			// init int
			//ppfTradepartnerService.initProfInt(layoutConfig, $scope, ctrl);
			//ppfTradepartnerService.initProfCostOfFundInt(layoutConfig, $scope, ctrl);
			// init pastdue
			ppfTradepartnerService.initProfPastdue(layoutConfig, $scope, ctrl);
			// init fee
			ppfTradepartnerService.initProfFee(layoutConfig, $scope, ctrl);
			// init doc
			ppfTradepartnerService.initEventDoccfg(layoutConfig, $scope, ctrl);
			// init limit facility
			//ppfTradepartnerService.initProfLimitFaci(layoutConfig, $scope, ctrl);
			//ppfTradepartnerService.initProfLimitInsFaci(layoutConfig, $scope, ctrl);
			//ppfTradepartnerService.initProfLimitPurFaci(layoutConfig, $scope, ctrl);
			// init Settlement Instruction
			ppfTradepartnerService.initProfSetlinst(layoutConfig, $scope, ctrl);
			// init cust prop define
			ppfTradepartnerService.initCustProp(layoutConfig, $scope, ctrl);
			//
			ppfTradepartnerService.initGeneral($scope, ctrl).then(function() {
				authService.createVerify(params).then(function(response) {
					var initPpfTradepartner = response.data.model;
					ctrl.action = "createVerify";
					ppfTradepartnerService.initLayoutConfigAndModel($scope, ctrl, initPpfTradepartner, ctrl.action, false).then(function() {
						// ctrl.ppfTradepartner = initPpfTradepartner; done in initLayoutConfig
						ctrl.ppfTradepartner.taskId = response.data.task.taskId || ppfTradepartnerInfo.taskId;
					});
				});
			});
		};

		ctrl.createVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [$filter("encode")(ctrl.ppfTradepartner.sellerCode)]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createVerifyApprove(ctrl.ppfTradepartner).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		ctrl.createVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.ppfTradepartner.comments = comments;
						authService.createVerifyReject(ctrl.ppfTradepartner).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};
		
		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.ppftradepartner.list',
					'state' : 'cda.ppfTradepartner.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.ppftradepartner.list',
					'state' : 'cda.ppfTradepartner.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);